<cfcomponent output="false">
	
	<cfset ignore = {
		'the':1,'be':1,'to':1,'of':1,'and':1,'a':1,'in':1,'that':1,'have':1,'I':1,'it':1,'for':1,'not':1,'on':1,'with':1,'he':1,'
		as':1,'you':1,'do':1,'at':1,'this':1,'but':1,'his':1,'by':1,'from':1,'they':1,'we':1,'her':1,'she':1,'or':1,'an':1,'will':1,'
		my':1,'all':1,'would':1,'there':1,'their':1,'what':1,'so':1,'up':1,'out':1,'if':1,'me':1,'like':1,'no':1,'him':1,'your':1,'
		some':1,'them':1,'than':1,'then':1,'its':1,'also':1,'our':1,'because':1,'these':1,'us':1,'is':1,'are':1
	} />
	
	<!--- --->

	<cffunction name="saveDiscussion" access="public" output="true" returntype="string">
		<cfargument name="discussionID" required="false" default="" />
		<cfargument name="discussionCategoryID" required="true" />
		<cfargument name="name" required="false" default="" />

		<cfset var local = {} />

		<cfset local.id = $.database.save(
			table="discussion",
			id=arguments.discussionID,
			discussion_category_id=arguments.discussionCategoryID,
			name=arguments.name
		) />
		
		<cfif arguments.discussionID eq "">
			<cfset $.database.save(
				table="discussion_user",
				id="",
				user_id=request.session.user.id,
				discussion_id=local.id
			) />
		</cfif>

		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveMessage" access="public" output="true" returntype="string">
		<cfargument name="discussionMessageID" required="false" default="" />
		<cfargument name="discussionID" required="true" />
		<cfargument name="message" required="true" />

		<cfset var local = {} />
		
		<cftransaction>
			<cfset local.id = $.database.save(
				table="discussion_message",
				id=arguments.discussionMessageID,
				user_id=request.session.user.id,
				discussion_id=arguments.discussionID,
				message=arguments.message
			) />
			<!--- set the discussion's archive flag to 0 each time a message is added --->
			<cfset $.database.save(
				table="discussion",
				id=arguments.discussionID,
				archive=0		
			) />
			<!--- mark your own message as read --->
			<cfset $.run.it('discussionService.markAsRead', {discussionMessageIDs=local.id}) />		
		</cftransaction>
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveInitialDiscussion" access="public" output="true" returntype="string">

		<cfset var local = {} />
		
		<cfif arguments.name neq "">
			<cfset arguments.discussionID = $.run.it('discussionService.saveDiscussion',arguments) />
			<cfif arguments.message neq "">
				<cfset $.run.it('discussionService.saveMessage',arguments) />
			</cfif>
		</cfif>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getActiveDiscussions" access="public" output="true" returntype="query">
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		<cfargument name="order" required="false" default="ifnull(message_date.max_date,discussion_user.created) desc" />

		<!--- <cfreturn getDiscussions(argumentCollection=arguments) /> --->
		<cfreturn $.run.it('discussionService.getDiscussions', arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getMessages" access="public" output="true" returntype="query">
		<cfargument name="discussionID" required="false" default="" />

		<cfreturn application.g.discussionGateway.getMessages(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="unfollowDiscussion" access="public" output="true" returntype="string">
		<cfargument name="discussionID" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.unfollowDiscussion" datasource="#$.site.getVar('datasource')#">
			update discussion_user
			set active = 0
			where discussion_user.active = 1
			and discussion_user.discussion_id = #$.database.uuid(listGetAt(arguments.discussionID,1))# 
			and discussion_user.user_id = #$.database.uuid(request.session.user.id)#
		</cfquery>

		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getDiscussionCategories" access="public" output="true" returntype="query">
		
		<cfreturn application.g.discussionGateway.getDiscussionCategories(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getDiscussions" access="public" output="true" returntype="query">

		<cfreturn application.g.discussionGateway.getDiscussions(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="followDiscussion" access="public" output="true" returntype="string">
		<cfargument name="discussionID" required="true" />
		
		<cfset var local = {} />
		
		<cfset $.run.it('discussionService.unfollowDiscussion',arguments) />
		
		<cftransaction>
			<cfset $.database.save(
				table="discussion_user",
				discussion_id=arguments.discussionID,
				user_id=request.session.user.id		
			) />
			
			<!--- set the discussion's archive flag to 0 each time a message is added --->
			<cfset $.database.save(
				table="discussion",
				id=arguments.discussionID,
				archive=0
			) />	
		</cftransaction>

		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="markAsRead" access="public" output="true" returntype="string">
		<cfargument name="discussionMessageIDs" required="true" />

		<cfset var local = {} />
		
		<cfloop list="#arguments.discussionMessageIDs#" index="local.i">
			<cfset $.database.save(
				table="discussion_message_user_read",
				discussion_message_id=local.i,
				user_id=request.session.user.id		
			) />
		</cfloop>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getSimilarDiscussions" access="public" output="true" returntype="query">
		<cfargument name="discussionCategoryID" required="true" />
		<cfargument name="string" required="true" />
		
		<cfset var local = {} />
				
		<!--- remove punctuation --->
		<cfset local.string = arguments.string />
		<cfset local.string = rereplace(local.string,"[^0-9A-Za-z ]"," ","ALL") />
		<!--- get wordlist --->
		<cfset local.stringArray = listToArray(local.string," ") />
		<cfset arguments.okWords = {} />

		<!--- remove common words --->
		<cfloop array="#local.stringArray#" item="local.item" index="local.i">
			<cfif not structKeyExists(ignore,local.item)>
				<cfset arguments.okWords[local.item] = 1 />
				<cfif right(local.item,2) eq 'es'>
					<cfset arguments.okWords[left(local.item,len(local.item)-2)] = 1 />
				<cfelseif right(local.item,1) eq 's'>
					<cfset arguments.okWords[left(local.item,len(local.item)-1)] = 1 />
				</cfif>
			</cfif>
		</cfloop>
		
		<cfreturn application.g.discussionGateway.getSimilarDiscussions(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteDiscussion" access="public" output="true" returntype="void">
		<cfargument name="discussionID" required="true" />
		
		<cfset var local = {} />
				
		<cfset $.database.delete(
			table="discussion",
			id=arguments.discussionID		
		) />
				
	</cffunction>
	
	<!--- --->

</cfcomponent>